package org.mech.sefer.entity.model;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * <p>
 * The AbstractEntity that implements standard identity based {@link #hashCode()}
 * and {@link #equals(Object)} functions
 * </p>
 * <p>
 * Date: 4.5.2012 10:51:43
 * </p>
 * 
 * @author martin.krajc
 */
@SuppressWarnings("serial")
public abstract class AbstractEntity implements Serializable {

	/**
	 * Gets the id of entity
	 *
	 * @return the id
	 */
	protected abstract Serializable getId();

	@Override
	public int hashCode() {
		return getId() == null ? null : new HashCodeBuilder(17, 37).append(
				getId()).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		AbstractEntity rhs = (AbstractEntity) obj;
		return new EqualsBuilder().appendSuper(super.equals(obj))
				.append(getId(), rhs.getId()).isEquals();
	}
}
